<?php
declare (strict_types = 1);

namespace app\common\model;

use think\Model;
use think\facade\Db;


/**
 * 浏览记录
 * @mixin \think\Model
 */
class Browse extends Model
{
	/**
	 * 企业浏览
	 * @return [type] [description]
	 */
	public function compborwse($company_id)
	{
        $result = self::alias('b')
            ->join('member m', 'b.member_id = m.id','LEFT')
            ->join('member_detail d', 'd.member_id = m.id','LEFT')
            ->where('company_id',$company_id)
            ->where('type',2)
            ->field('m.id,m.name,m.avatar,m.sex,d.advantage,m.participation_time,m.education_level')
            ->order('b.id desc')
            ->paginate(10);

        $result = $result->toArray();

        foreach ($result['data'] as $key => $value) {
        	// 求职意向
            $objective = $this->objectives($value['id']);

            if($objective) {
                $result['data'][$key]['jobname'] = $objective['name'];
            }
            // 经验
            $result['data'][$key]['participation_num'] = diffDate($value['participation_time'],time());
            // 性别
            $result['data'][$key]['sex'] = sextype($value['sex']);
        }

        return ['list'=>$result];
	}

    /**
     * 用户浏览
     * @return [type] [description]
     */
    public function userborwse($company_id)
    {
        $result = self::alias('b')
            ->join('member m', 'b.member_id = m.id','LEFT')
            ->join('member_detail d', 'd.member_id = m.id','LEFT')
            ->join('job j', 'b.job_id = j.id','LEFT')
            ->where('b.company_id',$company_id)
            ->where('type',1)
            ->field('m.id,m.name,m.avatar,m.sex,d.advantage,m.participation_time,j.title as jobname,m.education_level')
            ->order('b.id desc')
            ->paginate(10);

        $result = $result->toArray();

        foreach ($result['data'] as $key => $value) {
            // 求职意向
            $objective = $this->objectives($value['id']);

            if($objective) {
                $result['data'][$key]['jobname'] = $objective['name'];
            }
            // 经验
            if (!!$value['participation_time']) {
                $result['data'][$key]['participation_num'] = diffDate($value['participation_time'],time());
            } else {
                $result['data'][$key]['participation_num'] = '';
            }
            
            if (!$value['name']) {
                $result['data'][$key]['name'] = '牛人';
            }
            // 性别
            $result['data'][$key]['sex'] = sextype($value['sex']);
        }

        return ['list'=>$result];
    }


    /**
     * 用户端  企业浏览
     * @return [type] [description]
     */
    public function user_borwse($member_id)
    {
        $result = self::alias('b')
            ->join('company c', 'c.id = b.company_id','LEFT')
            ->where('b.member_id',$member_id)
            ->where('type',2)
            ->group('company_id')
            ->field('b.company_id as id,c.company_name as name,c.logo,c.financing,c.scale,c.time_rest,c.short_name,c.area_no')
            ->order('b.id desc')
            ->paginate(10);

        $result = $result->toArray();

        foreach ($result['data'] as $key => $value) {
            $result['data'][$key]['area_no'] = areaNoText($value['area_no']); // 上班地区
            $result['data'][$key]['jobname'] = '';
            $result['data'][$key]['financing'] = financingtype($value['financing']);
            $result['data'][$key]['scale'] = scaletype($value['scale']);
            $result['data'][$key]['time_rest'] = time_resttype($value['time_rest']);

            $management = Db::table('ea_company_management c')
                ->join('ea_member m','c.member_id = m.id')
                ->where('c.company_id',$value['id'])
                ->field('m.name,position,m.avatar,sex')
                ->find();


            $result['data'][$key]['personnel_name'] = $management['name'] ? $management['name'] : '招聘员';
            $result['data'][$key]['position'] = $management['position'] ? $management['position'] : '人事部';
            $result['data'][$key]['avatar'] = $management['avatar'];
        }

        return ['list'=>$result];
    }

    /**
     * 求职意向
     * @return [type] [description]
     */
    public function objectives($id)
    {
        $objective = Db::table('ea_objective')->where('member_id',$id)->field('nature,area_no,name,expect')->order('id desc')->limit(1)->find();

        if($objective) {
            $objective['area_no_text'] = areaNoText($objective['area_no']);
        }

        return $objective;
    }
}
